package com.czl.module_service.viewmodel

import androidx.databinding.ObservableField
import com.blankj.utilcode.util.GsonUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.tengyun.AssetHandleFacilityBean
import com.czl.base.data.bean.tengyun.ListBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

class AssetHandleFacilityInuseViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    var parentCompanyId: Int = 0
    var currentPage = 1
    var pageSize = 10
    val uc = UiChangeEvent()
    var beanList: MutableList<AssetHandleFacilityBean> = arrayListOf()
    var facilityNoObs = ObservableField("")
    var facilityNameObs = ObservableField("")

    class UiChangeEvent {
        val loadCompleteEvent: SingleLiveEvent<ListBean<AssetHandleFacilityBean>> = SingleLiveEvent()
        val loadSubmitEvent: SingleLiveEvent<MutableList<AssetHandleFacilityBean>> = SingleLiveEvent()
        val loadChangeEvent: SingleLiveEvent<Int> = SingleLiveEvent()
    }
    val reSearch: BindingCommand<Void> = BindingCommand(BindingAction {
        uc.loadChangeEvent.postValue(0)
    })
    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        this.selectFacilityListInUseByCompanyIdAndUserId()
    })
    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        currentPage = 1
        this.selectFacilityListInUseByCompanyIdAndUserId()
    })

    val selectedSubmit: BindingCommand<Void> = BindingCommand(BindingAction {
        var selectedList: MutableList<AssetHandleFacilityBean>? = arrayListOf()
        for (bean in beanList!!) {
            if (bean.selected!=null && bean.selected!!) {
                selectedList?.add(bean)
            }
        }
        uc.loadSubmitEvent.postValue(selectedList)
    })
    fun getDataList(){
        this.selectFacilityListInUseByCompanyIdAndUserId()
    }

    private fun selectFacilityListInUseByCompanyIdAndUserId(
        companyId: String? = model.getLoginCompanyId(),
        userId: String? = model.getLocalUserInfo()?.userId,
        titleName: String? = null
    ) {
        val facilityName: String? = facilityNameObs.get()
        val facilityNo: String? = facilityNoObs.get()
        val handleApplicationId: String? = null
        val type: Int? = 1
        model.mTengYunHttpDataSource.selectFacilityListInUseByCompanyIdAndUserId(
            GsonUtils.toJson(
                mapOf(
                    "pageNo" to currentPage,
                    "pageSize" to pageSize,
                    "userId" to userId,
                    "companyId" to companyId,
                    "facilityName" to facilityName,
                    "facilityNo" to facilityNo,
                    "handleApplicationId" to handleApplicationId,
                    "parentCompanyId" to parentCompanyId,
                    "type" to type
                )
            )
        ).compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<ListBean<AssetHandleFacilityBean>>>() {
                override fun onResult(t: BaseBean<ListBean<AssetHandleFacilityBean>>) {
                    if (t.state == 0) {
                        if (currentPage == 1) {
                            beanList.clear()
                        }
                        beanList.addAll(t.data?.list!!)
                        currentPage++
                        uc.loadCompleteEvent.postValue(t.data)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }
}